home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxifast.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  21.1 KB  |  751 lines

  1. /* Copyright (C) 1989, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxifast.c,v 1.2 2000/09/19 19:00:37 lpd Exp $ */
  20. /* Fast monochrome image rendering */
  21. #include "gx.h"
  22. #include "memory_.h"
  23. #include "gpcheck.h"
  24. #include "gsbittab.h"
  25. #include "gserrors.h"
  26. #include "gxfixed.h"
  27. #include "gxarith.h"
  28. #include "gxmatrix.h"
  29. #include "gsccolor.h"
  30. #include "gspaint.h"
  31. #include "gsutil.h"
  32. #include "gxdevice.h"
  33. #include "gxcmap.h"
  34. #include "gxdcolor.h"
  35. #include "gxistate.h"
  36. #include "gxdevmem.h"
  37. #include "gdevmem.h"        /* for mem_mono_device */
  38. #include "gxcpath.h"
  39. #include "gximage.h"
  40. #include "gzht.h"
  41.  
  42. /* Conditionally include statistics code. */
  43. #ifdef DEBUG
  44. #  define STATS
  45. #endif
  46.  
  47. /* ------ Strategy procedure ------ */
  48.  
  49. /* Check the prototype. */
  50. iclass_proc(gs_image_class_1_simple);
  51.  
  52. /* Use special fast logic for portrait or landscape black-and-white images. */
  53. private irender_proc(image_render_skip);
  54. private irender_proc(image_render_simple);
  55. private irender_proc(image_render_landscape);
  56. irender_proc_t
  57. gs_image_class_1_simple(gx_image_enum * penum)
  58. {
  59.     irender_proc_t rproc;
  60.     fixed ox = dda_current(penum->dda.pixel0.x);
  61.     fixed oy = dda_current(penum->dda.pixel0.y);
  62.  
  63.     if (penum->use_rop || penum->spp != 1 || penum->bps != 1)
  64.     return 0;
  65.     switch (penum->posture) {
  66.     case image_portrait:
  67.         {            /* Use fast portrait algorithm. */
  68.         long dev_width =
  69.             fixed2long_pixround(ox + penum->x_extent.x) -
  70.             fixed2long_pixround(ox);
  71.  
  72.         if (dev_width != penum->rect.w) {
  73.             /*
  74.              * Add an extra align_bitmap_mod of padding so that
  75.              * we can align scaled rows with the device.
  76.              */
  77.             long line_size =
  78.             bitmap_raster(any_abs(dev_width)) + align_bitmap_mod;
  79.  
  80.             if (penum->adjust != 0 || line_size > max_uint)
  81.             return 0;
  82.             /* Must buffer a scan line. */
  83.             penum->line_width = any_abs(dev_width);
  84.             penum->line_size = (uint) line_size;
  85.             penum->line = gs_alloc_bytes(penum->memory,
  86.                         penum->line_size, "image line");
  87.             if (penum->line == 0) {
  88.             gx_default_end_image(penum->dev,
  89.                          (gx_image_enum_common_t *)penum,
  90.                          false);
  91.             return 0;
  92.             }
  93.         }
  94.         if_debug2('b', "[b]render=simple, unpack=copy; rect.w=%d, dev_width=%ld\n",
  95.               penum->rect.w, dev_width);
  96.         rproc = image_render_simple;
  97.         break;
  98.         }
  99.     case image_landscape:
  100.         {            /* Use fast landscape algorithm. */
  101.         long dev_width =
  102.             fixed2long_pixround(oy + penum->x_extent.y) -
  103.             fixed2long_pixround(oy);
  104.         long line_size =
  105.             (dev_width = any_abs(dev_width),
  106.              bitmap_raster(dev_width) * 8 +
  107.              ROUND_UP(dev_width, 8) * align_bitmap_mod);
  108.  
  109.         if ((dev_width != penum->rect.w && penum->adjust != 0) ||
  110.             line_size > max_uint
  111.             )
  112.             return 0;
  113.         /* Must buffer a group of 8N scan lines. */
  114.         penum->line_width = dev_width;
  115.         penum->line_size = (uint) line_size;
  116.         penum->line = gs_alloc_bytes(penum->memory,
  117.                          penum->line_size, "image line");
  118.         if (penum->line == 0) {
  119.             gx_default_end_image(penum->dev,
  120.                      (gx_image_enum_common_t *) penum,
  121.                      false);
  122.             return 0;
  123.         }
  124.         penum->xi_next = penum->line_xy = fixed2int_var_rounded(ox);
  125.         if_debug3('b', "[b]render=landscape, unpack=copy; rect.w=%d, dev_width=%ld, line_size=%ld\n",
  126.               penum->rect.w, dev_width, line_size);
  127.         rproc = image_render_landscape;
  128.         /* Precompute values needed for rasterizing. */
  129.         penum->dxy =
  130.             float2fixed(penum->matrix.xy +
  131.                 fixed2float(fixed_epsilon) / 2);
  132.         break;
  133.         }
  134.     default:
  135.         return 0;
  136.     }
  137.     /* Precompute values needed for rasterizing. */
  138.     penum->dxx =
  139.     float2fixed(penum->matrix.xx + fixed2float(fixed_epsilon) / 2);
  140.     /*
  141.      * We don't want to spread the samples, but we have to reset unpack_bps
  142.      * to prevent the buffer pointer from being incremented by 8 bytes per
  143.      * input byte.
  144.      */
  145.     penum->unpack = sample_unpack_copy;
  146.     penum->unpack_bps = 8;
  147.     if (penum->use_mask_color) {
  148.     /* Adjust the colors and mask status accordinate to the mask color. */
  149.     penum->masked = true;
  150.     if (penum->mask_color.values[0] == 1) {
  151.         /* v0 = v1 = 1, 1 is transparent. */
  152.         memcpy(&penum->map[0].table.lookup4x1to32[0],
  153.            lookup4x1to32_inverted, 16 * 4);
  154.         penum->icolor1 = penum->icolor0;
  155.     } else if (penum->mask_color.values[1] == 0) {
  156.         /* v0 = v1 = 0, 0 is transparent. */
  157.         memcpy(&penum->map[0].table.lookup4x1to32[0],
  158.            lookup4x1to32_identity, 16 * 4);
  159.     } else {
  160.         /*
  161.          * The only other possible in-range value is v0 = 0, v1 = 1.
  162.          * The image is completely transparent!
  163.          */
  164.         rproc = image_render_skip;
  165.     }
  166.     color_set_pure(&penum->icolor0, gx_no_color_index);
  167.     penum->map[0].decoding = sd_none;
  168.     }
  169.     return rproc;
  170. }
  171.  
  172. /* ------ Rendering procedures ------ */
  173.  
  174. #define DC_IS_NULL(pdc)\
  175.   (gx_dc_is_pure(pdc) && (pdc)->colors.pure == gx_no_color_index)
  176.  
  177. /* Skip over a completely transparent image. */
  178. private int
  179. image_render_skip(gx_image_enum * penum, const byte * buffer, int data_x,
  180.           uint w, int h, gx_device * dev)
  181. {
  182.     return h;
  183. }
  184.  
  185. /*
  186.  * Scale (and possibly reverse) one scan line of a monobit image.
  187.  * This is used for both portrait and landscape image processing.
  188.  * We pass in an x offset (0 <= line_x < align_bitmap_mod * 8) so that
  189.  * we can align the result with the eventual device X.
  190.  *
  191.  * To be precise, the input to this routine is the w bits starting at
  192.  * bit data_x in buffer.  These w bits expand to abs(x_extent) bits,
  193.  * either inverted (zero = 0xff) or not (zero = 0), starting at bit
  194.  * line_x in line which corresponds to coordinate
  195.  * fixed2int_pixround(xcur + min(x_extent, 0)).  Note that the entire
  196.  * bytes containing the first and last output bits are affected: the
  197.  * other bits in those bytes are set to zero (i.e., the value of the
  198.  * 'zero' argument).
  199.  */
  200. #ifdef STATS
  201. struct stats_image_fast_s {
  202.     long
  203.          calls, all0s, all1s, runs, lbit0, byte00, byte01, byte02, byte03,
  204.          byte04, rbit0, lbit1, byte1, rbit1, thin, thin2, nwide, bwide,
  205.          nfill, bfill;
  206. } stats_image_fast;
  207. #  define INCS(stat) ++stats_image_fast.stat
  208. #  define ADDS(stat, n) stats_image_fast.stat += n
  209. #else
  210. #  define INCS(stat) DO_NOTHING
  211. #  define ADDS(stat, n) DO_NOTHING
  212. #endif
  213. inline private void
  214. fill_row(byte *line, int line_x, uint raster, int value)
  215. {
  216.     memset(line + (line_x >> 3), value, raster - (line_x >> 3));
  217. }
  218. private void
  219. image_simple_expand(byte * line, int line_x, uint raster,
  220.             const byte * buffer, int data_x, uint w,
  221.             fixed xcur, fixed x_extent, byte zero /* 0 or 0xff */ )
  222. {
  223.     int dbitx = data_x & 7;
  224.     byte sbit = 0x80 >> dbitx;
  225.     byte sbitmask = 0xff >> dbitx;
  226.     uint wx = dbitx + w;
  227.     gx_dda_fixed xl;
  228.     gx_dda_step_fixed dxx4, dxx8, dxx16, dxx24, dxx32;
  229.     register const byte *psrc = buffer + (data_x >> 3);
  230.  
  231.     /*
  232.      * The following 3 variables define the end of the input data row.
  233.      * We would put them in a struct, except that no compiler that we
  234.      * know of will optimize individual struct members as though they
  235.      * were simple variables (e.g., by putting them in registers).
  236.      *
  237.      * endp points to the byte that contains the bit just beyond the
  238.      * end of the row.  endx gives the bit number of this bit within
  239.      * the byte, with 0 being the *least* significant bit.  endbit is
  240.      * a mask for this bit.
  241.      */
  242.     const byte *endp = psrc + (wx >> 3);
  243.     int endx = ~wx & 7;
  244.     byte endbit = 1 << endx;
  245.  
  246.     /*
  247.      * The following 3 variables do the same for start of the last run
  248.      * of the input row (think of it as a pointer to just beyond the
  249.      * end of the next-to-last run).
  250.      */
  251.     const byte *stop = endp;
  252.     int stopx;
  253.     byte stopbit = endbit;
  254.     byte data;
  255.     byte one = ~zero;
  256.     fixed xl0;
  257.  
  258.     if (w == 0)
  259.     return;
  260.     INCS(calls);
  261.  
  262.     /* Scan backward for the last transition. */
  263.     if (stopbit == 0x80)
  264.     --stop, stopbit = 1;
  265.     else
  266.     stopbit <<= 1;
  267.     /* Now (stop, stopbit) give the last bit of the row. */
  268.     {
  269.     byte stopmask = -stopbit << 1;
  270.     byte last = *stop;
  271.  
  272.     if (stop == psrc)    /* only 1 input byte */
  273.         stopmask &= sbitmask;
  274.     if (last & stopbit) {
  275.         /* The last bit is a 1: look for a 0-to-1 transition. */
  276.         if (~last & stopmask) {    /* Transition in last byte. */
  277.         last |= stopbit - 1;
  278.         } else {        /* No transition in the last byte. */
  279.         while (stop > psrc && stop[-1] == 0xff)
  280.             --stop;
  281.         if (stop == psrc ||
  282.             (stop == psrc + 1 && !(~*psrc & sbitmask))
  283.             ) {
  284.             /* The input is all 1s.  Clear the row and exit. */
  285.             INCS(all1s);
  286.             fill_row(line, line_x, raster, one);
  287.             return;
  288.         }
  289.         last = *--stop;
  290.         }
  291.         stopx = byte_bit_run_length_0[byte_reverse_bits[last]] - 1;
  292.     } else {
  293.         /* The last bit is a 0: look for a 1-to-0 transition. */
  294.         if (last & stopmask) {    /* Transition in last byte. */
  295.         last &= -stopbit;
  296.         } else {        /* No transition in the last byte. */
  297.         while (stop > psrc && stop[-1] == 0)
  298.             --stop;
  299.         if (stop == psrc ||
  300.             (stop == psrc + 1 && !(*psrc & sbitmask))
  301.             ) {
  302.             /* The input is all 0s.  Clear the row and exit. */
  303.             INCS(all0s);
  304.             fill_row(line, line_x, raster, zero);
  305.             return;
  306.         }
  307.         last = *--stop;
  308.         }
  309.         stopx = byte_bit_run_length_0[byte_reverse_bits[last ^ 0xff]] - 1;
  310.     }
  311.     if (stopx < 0)
  312.         stopx = 7, ++stop;
  313.     stopbit = 1 << stopx;
  314.     }
  315.  
  316.     /* Pre-clear the row. */
  317.     fill_row(line, line_x, raster, zero);
  318.  
  319.     /* Set up the DDAs. */
  320.     xl0 =
  321.     (x_extent >= 0 ?
  322.      fixed_fraction(fixed_pre_pixround(xcur)) :
  323.      fixed_fraction(fixed_pre_pixround(xcur + x_extent)) - x_extent);
  324.     xl0 += int2fixed(line_x);
  325.     dda_init(xl, xl0, x_extent, w);
  326.     dxx4 = xl.step;
  327.     dda_step_add(dxx4, xl.step);
  328.     dda_step_add(dxx4, dxx4);
  329.     dxx8 = dxx4;
  330.     dda_step_add(dxx8, dxx4);
  331.     dxx16 = dxx8;
  332.     dda_step_add(dxx16, dxx8);
  333.     dxx24 = dxx16;
  334.     dda_step_add(dxx24, dxx8);
  335.     dxx32 = dxx24;
  336.     dda_step_add(dxx32, dxx8);
  337.  
  338.     /*
  339.      * Loop invariants:
  340.      *      data = *psrc;
  341.      *      sbit = 1 << n, 0<=n<=7.
  342.      */
  343.     for (data = *psrc;;) {
  344.     int x0, n, bit;
  345.     byte *bp;
  346.     static const byte lmasks[9] = {
  347.         0xff, 0x7f, 0x3f, 0x1f, 0xf, 7, 3, 1, 0
  348.     };
  349.     static const byte rmasks[9] = {
  350.         0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
  351.     };
  352.  
  353.     INCS(runs);
  354.  
  355.     /* Scan a run of zeros. */
  356.     data ^= 0xff;        /* invert */
  357.     while (data & sbit) {
  358.         dda_next(xl);
  359.         sbit >>= 1;
  360.         INCS(lbit0);
  361.     }
  362.     if (!sbit) {        /* Scan a run of zero bytes. */
  363. sw:        if ((data = psrc[1]) != 0) {
  364.         psrc++;
  365.         INCS(byte00);
  366.         } else if ((data = psrc[2]) != 0) {
  367.         dda_state_next(xl.state, dxx8);
  368.         psrc += 2;
  369.         INCS(byte01);
  370.         } else if ((data = psrc[3]) != 0) {
  371.         dda_state_next(xl.state, dxx16);
  372.         psrc += 3;
  373.         INCS(byte02);
  374.         } else if ((data = psrc[4]) != 0) {
  375.         dda_state_next(xl.state, dxx24);
  376.         psrc += 4;
  377.         INCS(byte03);
  378.         } else {
  379.         dda_state_next(xl.state, dxx32);
  380.         psrc += 4;
  381.         INCS(byte04);
  382.         goto sw;
  383.         }
  384.         if (data > 0xf)
  385.         sbit = 0x80;
  386.         else {
  387.         sbit = 0x08;
  388.         dda_state_next(xl.state, dxx4);
  389.         }
  390.         data ^= 0xff;    /* invert */
  391.         while (data & sbit) {
  392.         dda_next(xl);
  393.         sbit >>= 1;
  394.         INCS(rbit0);
  395.         }
  396.     }
  397.     x0 = dda_current_fixed2int(xl);
  398.     if (psrc >= stop && sbit == stopbit) {
  399.         /*
  400.          * We've scanned the last run of 0s.
  401.          * Prepare to fill the final run of 1s.
  402.          */
  403.         n = fixed2int(xl0 + x_extent) - x0;
  404.     } else {        /* Scan a run of ones. */
  405.         /* We know the current bit is a one. */
  406.         data ^= 0xff;    /* un-invert */
  407.         do {
  408.         dda_next(xl);
  409.         sbit >>= 1;
  410.         INCS(lbit1);
  411.         }
  412.         while (data & sbit);
  413.         if (!sbit) {    /* Scan a run of 0xff bytes. */
  414.         while ((data = *++psrc) == 0xff) {
  415.             dda_state_next(xl.state, dxx8);
  416.             INCS(byte1);
  417.         }
  418.         if (data < 0xf0)
  419.             sbit = 0x80;
  420.         else {
  421.             sbit = 0x08;
  422.             dda_state_next(xl.state, dxx4);
  423.         }
  424.         while (data & sbit) {
  425.             dda_next(xl);
  426.             sbit >>= 1;
  427.             INCS(rbit1);
  428.         }
  429.         }
  430.         n = dda_current_fixed2int(xl) - x0;
  431.     }
  432.  
  433.     /* Fill the run in the scan line. */
  434.     if (n < 0)
  435.         x0 += n, n = -n;
  436.     bp = line + (x0 >> 3);
  437.     bit = x0 & 7;
  438.     if ((n += bit) <= 8) {
  439.         *bp ^= lmasks[bit] - lmasks[n];
  440.         INCS(thin);
  441.     } else if ((n -= 8) <= 8) {
  442.         *bp ^= lmasks[bit];
  443.         bp[1] ^= rmasks[n];
  444.         INCS(thin2);
  445.     } else {
  446.         *bp++ ^= lmasks[bit];
  447.         if (n >= 56) {
  448.         int nb = n >> 3;
  449.  
  450.         memset(bp, one, nb);
  451.         bp += nb;
  452.         INCS(nwide);
  453.         ADDS(bwide, nb);
  454.         } else {
  455.         ADDS(bfill, n >> 3);
  456.         while ((n -= 8) >= 0)
  457.             *bp++ = one;
  458.         INCS(nfill);
  459.         }
  460.         *bp ^= rmasks[n & 7];
  461.     }
  462.     if (psrc >= stop && sbit == stopbit)
  463.         break;
  464.     }
  465. }
  466.  
  467. /* Copy one rendered scan line to the device. */
  468. private int
  469. copy_portrait(gx_image_enum * penum, const byte * data, int dx, int raster,
  470.           int x, int y, int w, int h, gx_device * dev)
  471. {
  472.     const gx_device_color *pdc0;
  473.     const gx_device_color *pdc1;
  474.     uint align = ALIGNMENT_MOD(data, align_bitmap_mod);
  475.  
  476.     /*
  477.      * We know that the lookup table maps 1 bit to 1 bit,
  478.      * so it can only have 2 states: straight-through or invert.
  479.      */
  480.     if (penum->map[0].table.lookup4x1to32[0])
  481.     pdc0 = &penum->icolor1, pdc1 = &penum->icolor0;
  482.     else
  483.     pdc0 = &penum->icolor0, pdc1 = &penum->icolor1;
  484.     data -= align;
  485.     dx += align << 3;
  486.     if (gx_dc_is_pure(pdc0) && gx_dc_is_pure(pdc1)) {
  487.     /* Just use copy_mono. */
  488.     dev_proc_copy_mono((*copy_mono)) =
  489.         (h == 1 || (raster & (align_bitmap_mod - 1)) == 0 ?
  490.          dev_proc(dev, copy_mono) : gx_copy_mono_unaligned);
  491.     return (*copy_mono)
  492.         (dev, data, dx, raster, gx_no_bitmap_id,
  493.          x, y, w, h, pdc0->colors.pure, pdc1->colors.pure);
  494.     }
  495.     /*
  496.      * At least one color isn't pure: if the other one is transparent, use
  497.      * the opaque color's fill_masked procedure.  Note that we use a
  498.      * slightly unusual representation for transparent here (per
  499.      * gx_begin_image1): a pure color with pixel value gx_no_color_index.
  500.      */
  501.     {
  502.     const gx_device_color *pdc;
  503.     bool invert;
  504.  
  505.     if (DC_IS_NULL(pdc1)) {
  506.         pdc = pdc0;
  507.         invert = true;
  508.     } else {
  509.         if (!DC_IS_NULL(pdc0)) {
  510.         int code = gx_device_color_fill_rectangle
  511.             (pdc0, x, y, w, h, dev, lop_default, NULL);
  512.  
  513.         if (code < 0)
  514.             return code;
  515.         }
  516.         pdc = pdc1;
  517.         invert = false;
  518.     }
  519.     return (*pdc->type->fill_masked)
  520.         (pdc, data, dx, raster, gx_no_bitmap_id, x, y, w, h,
  521.          dev, lop_default, invert);
  522.  
  523.     }
  524. }
  525.  
  526. /* Rendering procedure for a monobit image with no */
  527. /* skew or rotation and pure colors. */
  528. private int
  529. image_render_simple(gx_image_enum * penum, const byte * buffer, int data_x,
  530.             uint w, int h, gx_device * dev)
  531. {
  532.     dev_proc_copy_mono((*copy_mono)) = dev_proc(dev, copy_mono);
  533.     const fixed dxx = penum->dxx;
  534.     const byte *line;
  535.     uint line_width, line_size;
  536.     int line_x;
  537.     fixed xcur = dda_current(penum->dda.pixel0.x);
  538.     int ix = fixed2int_pixround(xcur);
  539.     int ixr;
  540.     const int iy = penum->yci, ih = penum->hci;
  541.     gx_device_color * const pdc0 = &penum->icolor0;
  542.     gx_device_color * const pdc1 = &penum->icolor1;
  543.     int dy;
  544.     int code;
  545.  
  546.     if (h == 0)
  547.     return 0;
  548.     if ((!DC_IS_NULL(pdc0) &&
  549.      (code = gx_color_load(pdc0, penum->pis, dev)) < 0) ||
  550.     (!DC_IS_NULL(pdc1) &&
  551.      (code = gx_color_load(pdc1, penum->pis, dev)) < 0)
  552.     )
  553.     return code;
  554.     if (penum->line == 0) {    /* A direct BitBlt is possible. */
  555.     line = buffer;
  556.     line_size = (w + 7) >> 3;
  557.     line_width = w;
  558.     line_x = 0;
  559.     } else if (copy_mono == dev_proc(&mem_mono_device, copy_mono) &&
  560.            dxx > 0 && gx_dc_is_pure(pdc1) && gx_dc_is_pure(pdc0) &&
  561.            /* We know the colors must be (0,1) or (1,0). */
  562.            (pdc0->colors.pure ^ pdc1->colors.pure) == 1 &&
  563.            !penum->clip_image &&
  564.            /*
  565.         * Even if clip_image is false, the clipping rectangle
  566.         * might lie partly outside the device coordinate space
  567.         * if the Margins values are non-zero.
  568.         */
  569.            ix >= 0 &&
  570.            (ixr = fixed2int_pixround(xcur + penum->x_extent.x) - 1) <
  571.              dev->width &&
  572.            iy >= 0 && iy + ih <= dev->height
  573.     ) {
  574.     /* Do the operation directly into the memory device bitmap. */
  575.     int line_ix;
  576.     int ib_left = ix >> 3, ib_right = ixr >> 3;
  577.     byte *scan_line = scan_line_base((gx_device_memory *) dev, iy);
  578.     byte save_left, save_right, mask;
  579.  
  580.     line_x = ix & (align_bitmap_mod * 8 - 1);
  581.     line_ix = ix - line_x;
  582.     line_size = (ixr >> 3) + 1 - (line_ix >> 3);
  583.     line_width = ixr + 1 - ix;
  584.     /* We must save and restore any unmodified bits in */
  585.     /* the two edge bytes. */
  586.     save_left = scan_line[ib_left];
  587.     save_right = scan_line[ib_right];
  588.     image_simple_expand(scan_line + (line_ix >> 3), line_x,
  589.                 line_size, buffer, data_x, w, xcur,
  590.                 penum->x_extent.x,
  591.                 ((pdc0->colors.pure == 0) !=
  592.                  (penum->map[0].table.lookup4x1to32[0] == 0) ?
  593.                  0xff : 0));
  594.     if (ix & 7)
  595.         mask = (byte) (0xff00 >> (ix & 7)),
  596.         scan_line[ib_left] =
  597.         (save_left & mask) + (scan_line[ib_left] & ~mask);
  598.     if ((ixr + 1) & 7)
  599.         mask = (byte) (0xff00 >> ((ixr + 1) & 7)),
  600.         scan_line[ib_right] =
  601.         (scan_line[ib_right] & mask) + (save_right & ~mask);
  602.     if (ih <= 1)
  603.         return 1;
  604.     /****** MAY BE UNALIGNED ******/
  605.     line = scan_line + (line_ix >> 3);
  606.     if (dxx < 0)
  607.         ix -= line_width;
  608.     for (dy = 1; dy < ih; dy++) {
  609.         int code = (*copy_mono)
  610.         (dev, line, line_x, line_size, gx_no_bitmap_id,
  611.          ix, iy + dy, line_width, 1,
  612.          (gx_color_index)0, (gx_color_index)1);
  613.  
  614.         if (code < 0)
  615.         return code;
  616.     }
  617.     return 0;
  618.     } else {
  619.     line = penum->line;
  620.     line_size = penum->line_size;
  621.     line_width = penum->line_width;
  622.     line_x = ix & (align_bitmap_mod * 8 - 1);
  623.     image_simple_expand(penum->line, line_x, line_size,
  624.                 buffer, data_x, w, xcur,
  625.                 penum->x_extent.x, 0);
  626.     }
  627.  
  628.     /* Finally, transfer the scan line to the device. */
  629.     if (dxx < 0)
  630.     ix -= line_width;
  631.     for (dy = 0; dy < ih; dy++) {
  632.     int code = copy_portrait(penum, line, line_x, line_size,
  633.                  ix, iy + dy, line_width, 1, dev);
  634.  
  635.     if (code < 0)
  636.         return code;
  637.     }
  638.  
  639.     return 1;
  640. }
  641.  
  642. /* Rendering procedure for a 90 degree rotated monobit image */
  643. /* with pure colors.  We buffer and then flip 8 scan lines at a time. */
  644. private int copy_landscape(P5(gx_image_enum *, int, int, bool, gx_device *));
  645. private int
  646. image_render_landscape(gx_image_enum * penum, const byte * buffer, int data_x,
  647.                uint w, int h, gx_device * dev)
  648. {
  649.     byte *line = penum->line;
  650.     uint raster = bitmap_raster(penum->line_width);
  651.     int ix = penum->xci, iw = penum->wci;
  652.     int xinc, xmod;
  653.     byte *row;
  654.     const byte *orig_row = 0;
  655.     bool y_neg = penum->dxy < 0;
  656.  
  657.     if (is_fneg(penum->matrix.yx))
  658.     ix += iw, iw = -iw, xinc = -1;
  659.     else
  660.     xinc = 1;
  661.     /*
  662.      * Because of clipping, there may be discontinuous jumps in the values
  663.      * of ix (xci).  If this happens, or if we are at the end of the data or
  664.      * a client has requested flushing, flush the flipping buffer.
  665.      */
  666.     if (ix != penum->xi_next || h == 0) {
  667.     int xi = penum->xi_next;
  668.     int code =
  669.         (xinc > 0 ?
  670.          copy_landscape(penum, penum->line_xy, xi, y_neg, dev) :
  671.          copy_landscape(penum, xi, penum->line_xy, y_neg, dev));
  672.  
  673.     if (code < 0)
  674.         return code;
  675.     penum->line_xy = penum->xi_next = ix;
  676.     if (h == 0)
  677.         return code;
  678.     }
  679.     for (; iw != 0; iw -= xinc) {
  680.     if (xinc < 0)
  681.         --ix;
  682.     xmod = ix & 7;
  683.     row = line + xmod * raster;
  684.     if (orig_row == 0) {
  685.         image_simple_expand(row, 0, raster,
  686.                 buffer, data_x, w,
  687.                 dda_current(penum->dda.pixel0.y),
  688.                 penum->x_extent.y, 0);
  689.         orig_row = row;
  690.     } else
  691.         memcpy(row, orig_row, raster);
  692.     if (xinc > 0) {
  693.         ++ix;
  694.         if (xmod == 7) {
  695.         int code =
  696.             copy_landscape(penum, penum->line_xy, ix, y_neg, dev);
  697.  
  698.         if (code < 0)
  699.             return code;
  700.         orig_row = 0;
  701.         penum->line_xy = ix;
  702.         }
  703.     } else {
  704.         if (xmod == 0) {
  705.         int code =
  706.             copy_landscape(penum, ix, penum->line_xy, y_neg, dev);
  707.  
  708.         if (code < 0)
  709.             return code;
  710.         orig_row = 0;
  711.         penum->line_xy = ix;
  712.         }
  713.     }
  714.     }
  715.     penum->xi_next = ix;
  716.     return 0;
  717. }
  718.  
  719. /* Flip and copy one group of scan lines. */
  720. private int
  721. copy_landscape(gx_image_enum * penum, int x0, int x1, bool y_neg,
  722.            gx_device * dev)
  723. {
  724.     byte *line = penum->line;
  725.     uint line_width = penum->line_width;
  726.     uint raster = bitmap_raster(line_width);
  727.     byte *flipped = line + raster * 8;
  728.     int w = x1 - x0;
  729.     int y = fixed2int_pixround(dda_current(penum->dda.pixel0.y));
  730.  
  731.     if (w == 0 || line_width == 0)
  732.     return 0;
  733.     /* Flip the buffered data from raster x 8 to align_bitmap_mod x */
  734.     /* line_width. */
  735.     if (line_width > 0) {
  736.     int i;
  737.  
  738.     for (i = (line_width - 1) >> 3; i >= 0; --i)
  739.         memflip8x8(line + i, raster,
  740.                flipped + (i << (log2_align_bitmap_mod + 3)),
  741.                align_bitmap_mod);
  742.     }
  743.     /* Transfer the scan lines to the device. */
  744.     if (w < 0)
  745.     x0 = x1, w = -w;
  746.     if (y_neg)
  747.     y -= line_width;
  748.     return copy_portrait(penum, flipped, x0 & 7, align_bitmap_mod,
  749.              x0, y, w, line_width, dev);
  750. }
  751.